/**
 * AirportResource.java
 * Jan 5, 2010
 */
package com.dlplay.flightinfo.server.resource;

import java.io.IOException;
import java.util.logging.Level;

import org.restlet.data.MediaType;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

import com.dlplay.flightinfo.core.FlightSystem;
import com.dlplay.flightinfo.data.Airport;
import com.dlplay.flightinfo.data.AirportTerminalCode;
import com.dlplay.flightinfo.data.CityCode;
import com.dlplay.flightinfo.presentation.XMLAirportGenerator;
import com.dlplay.flightinfo.presentation.XMLFlightGenerator;

/**
 * @author zhao yu
 *
 */
public class AirportResource extends ServerResource {
	
	private String terminal;
	private Airport airport;

	/*
	 * (non-Javadoc)
	 * @see org.restlet.resource.UniformResource#doInit()
	 */
	@Override
	protected void doInit() throws ResourceException {
		// Get the "itemName" attribute value taken from the URI template
        // /FlightInfo/{flightNum}
		
		terminal = (String) getRequest().getAttributes().get("terminal");
        AirportTerminalCode code = cityCodeMapping(terminal);
        // Get the item directly from the "persistence layer".
        airport = FlightSystem.getAirportManager().getAirport(code);
        setExisting(airport != null);
	}
	
	private AirportTerminalCode cityCodeMapping(String t) {
		// TODO todo_6, map terminal code
		return AirportTerminalCode.PEK_T3;
	}

	/*
	 * 
	 */
	 @Get("xml")
	    public Representation toXml() {
		try {
			getLogger().info("Create Flight XML");
			DomRepresentation representation = new DomRepresentation(
			         MediaType.TEXT_XML);
			XMLAirportGenerator generator = new XMLAirportGenerator(airport);
			generator.generate(representation.getDocument());
			return representation;
		} catch (IOException e) {
			getLogger().log(Level.SEVERE, "Create DomRepresentation Failed");
			e.printStackTrace();
		}
		return null;
		 
	 }
}
